<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

</body>
<script>
    function removeSpace(str){
        const len = str.length
        let first = 0
        let end = len - 1
        for(let i = first;i < len;i++){
            if (str[i] !== ' '){
                first = i
                break
            }
        }
        for(let i = end;i>0;i--){
            if(str[i] !== ' '){
                end = i
                break
            }
        }
        return str.slice(first,end +1)
    }

    const str = '    123 231234    '
    console.log(str.trim())
    console.log(removeSpace(str))
</script>
</html>